
/**
 * @Copyright 2015
 *
 **/
package cn.com.infcn.spider.util;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.nlpcn.commons.lang.util.StringUtil;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisSentinelPool;
import redis.clients.util.Pool;

/**
 * @author shb
 * @createTime 2015年1月15日
 */
public class JedisDao {
	
	private int threads = 200;
	
	private Pool<Jedis> pool = null;

	public JedisDao(String masterName, String hosts, int threads, String password) {
		Set<String> sentinels = new HashSet<>();
		List<String> asList = Arrays.asList(hosts.split("\\|"));
		sentinels.addAll(asList);
		GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
		poolConfig.setMaxTotal(threads);
		poolConfig.setMaxIdle(threads);
		this.threads = threads;
		pool = new JedisSentinelPool(masterName, sentinels, poolConfig, password);
	}

	public JedisDao(String host, int port, int threads, String password) {
		GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
		poolConfig.setMaxTotal(threads);
		poolConfig.setMaxIdle(threads);
		this.threads = threads;
		if (StringUtil.isBlank(password)) {
			pool = new JedisPool(poolConfig, host, port);
		} else {
			pool = new JedisPool(poolConfig, host, port, 5000, password);
		}
	}

	public JedisDao(String masterName, String hosts, int threads) {
		this(masterName, hosts, threads, null);
	}

	public JedisDao(String hosts, int port, int hreads) {
		this(hosts, port, 200, null);
	}

	public Jedis getJedis() {
		if (pool != null) {
			return pool.getResource();
		} else {
			return null;
		}
	}

	public int getThreadNum() {
		return threads;
	}

	/**
	 * 返还客户端对象
	 * 
	 * @param jedis
	 */
	public void returnResource(Jedis jedis) {
		if (pool != null) {
			pool.returnResource(jedis);
		}
	}

	public void returnBrokenResource(Jedis jedis) {
		if (pool != null) {
			pool.returnBrokenResource(jedis);
		}
	}

	/**
	 * 获取数据
	 * 
	 * @param key
	 * @return
	 */
	public List<String> mget(String[] key) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			return jedis.mget(key);
		} catch (Exception e) {
			// 释放redis对象
			returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				returnResource(jedis);
			}
		}
		return null;
	}

	public String lpop(String key) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			return jedis.lpop(key);
		} catch (Exception e) {
			// 释放redis对象
			returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				returnResource(jedis);
			}
		}
		return null;
	}

	public String rpop(String key) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			return jedis.rpop(key);
		} catch (Exception e) {
			// 释放redis对象
			returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				returnResource(jedis);
			}
		}
		return null;
	}

	/**
	 * @param string
	 * @param string2
	 */
	public void rpush(String key, String value) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			jedis.rpush(key, value);
		} catch (Exception e) {
			// 释放redis对象
			returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				returnResource(jedis);
			}
		}
	}

	/**
	 * @param string
	 * @param string2
	 */
	public void lpush(String key, String value) {
		Jedis jedis = null;
		try {
			jedis = getJedis();
			jedis.lpush(key, value);
		} catch (Exception e) {
			// 释放redis对象
			returnBrokenResource(jedis);
			e.printStackTrace();
		} finally {
			// 返还到连接池
			if (jedis != null) {
				returnResource(jedis);
			}
		}
	}
}
